﻿using CheckIn.UserInfoHost.Infrastructures;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheckIn.UserInfoHost.Controllers
{
    public class AdminController : BaseController<UserInfoDBContext>
    {
        private readonly WechatService _wechatService;

        public AdminController(WechatService wechatService)
        {
            _wechatService = wechatService;
        }

        /// <summary>
        /// 批量获取用户信息
        /// </summary>
        /// <param name="userids"></param>
        /// <returns></returns>
        public async Task<UserInfoExt[]> GetUserInfos(long[] userids)
        {
            return await (from m in this.CurrentDBContext.UserInfo
                          where userids.Contains(m.id.Value)
                          select new UserInfoExt
                          {
                              UserId = m.id.Value,
                              Avator = m.Avator,
                              Name = m.Name
                          }).ToArrayAsync();
        }

        /// <summary>
        /// 获取用户所有关注的uid
        /// </summary>
        /// <returns></returns>
        public async Task<long[]> GetFllowIds(long userid)
        {
            return await (from j in this.CurrentDBContext.UserFllow
                          where  j.UserId == userid
                          select j.TargetId).ToArrayAsync();
        }

        /// <summary>
        /// 判断一个用户是否关注另一个用户
        /// </summary>
        /// <param name="userid">关注人id</param>
        /// <param name="fllowUserId">被关注人id</param>
        /// <returns></returns>
        public async Task<bool> GetIsFllowed(long userid , long fllowUserId)
        {
            return await (from j in this.CurrentDBContext.UserFllow
                          where j.UserId == userid && j.TargetId == fllowUserId
                          select j).AnyAsync();
        }
        /// <summary>
        /// 获取用户关注人数
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public async Task<int> GetFllowCount(long userid)
        {
            return await (from j in this.CurrentDBContext.UserFllow
                          where j.UserId == userid
                          select j.TargetId).CountAsync();
        }

        /// <summary>
        /// 通知图片检测结果
        /// </summary>
        /// <param name="trace_id"></param>
        /// <param name="result"></param>
        public void NotifyImageCheckResult(string trace_id, TextChecResult result)
        {
            _wechatService.NotifyImageCheckResult(trace_id, result);    
        }
    }
}
